// pages/addressWrite/addressWrite.js
import {
  getAddressEdit,
  postAddressAdd,
  postAddressUpdate,
  getRestaurantAddress
} from '../../api/index'
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseUrl: app.globalData.baseUrl,
    addressData: {},
    id: -1,
    form: {
      street: '', //详细地址
      address: '', //门牌号
      name: '', //姓名
      mobile: '', //手机号
      state: 2 //是否默认
    },
    weidu: '', //毕成维度
    jingdu: '', //毕成经度
    juli: '', //距离
    juliBool: false //是否可以保存
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    getRestaurantAddress().then(res => { //获取目的地址经纬度
      if (res.state == 0) {
        this.data.weidu = res.data.weidu
        this.data.jingdu = res.data.jingdu
        this.data.juli = res.data.juli
      }
    })
    if (options.id) {
      // console.log(this.data.id)
      getAddressEdit(options.id).then(res => {
        if (res.state == 0) {
          var data = {
            id: res.data.id,
            name: res.data.name,
            mobile: res.data.mobile,
            street: res.data.street,
            address: res.data.address,
            state: res.data.state
          }
          this.setData({
            form: data,
            id: options.id,
            juliBool: true
          })
          console.log(res.data)
        }
      })
    } else {
      this.setData({
        juliBool: false
      })
    }

  },
  //详细地址 输入框
  bindInputStreet(e) {
    this.data.form.street = e.detail.value
    this.setData({
      form: this.data.form
    })
  },
  //详细地址
  onLocation() {

    // wx.getLocation({ //定位
    //   type: 'wgs84',
    //   success(res) {
    //     // const latitude = res.latitude
    //     // const longitude = res.longitude
    //     // const speed = res.speed
    //     // const accuracy = res.accuracy
    //     console.log(res)
    //   }
    // })
    var that = this;  
    //已经同意获取的了
    wx.chooseLocation({
      success: function (res) {
        // success
        console.log(res, "location")
        console.log(res.name)
        var weidu1 = res.latitude * Math.PI / 180.0 //纬度
        var jingdu1 = res.longitude //经度
        var weidu2 = that.data.weidu * Math.PI / 180.0 //起点维度
        var jingdu2 = that.data.jingdu //起点经度
        var a = weidu1 - weidu2
        var b = jingdu1 * Math.PI / 180.0 - jingdu2 * Math.PI / 180.0
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(weidu1) * Math.cos(weidu2) * Math.pow(Math.sin(b / 2), 2)))
        s = s * 6378.137;
        s = Math.round(s * 1000) //单位米
        if (s >= that.data.juli) { //距离大过配送范围  
          that.data.juliBool = false
        } else {
          that.data.juliBool = true
        }
        console.log("s+++++++", s)
        console.log(res.latitude)
        console.log(res.longitude)
        that.data.form.street = res.name
        that.setData({
          form: that.data.form
        })
        console.log(that.data.form.street)
      },
      fail: function () {     //取消获取
        wx.getSetting({
          success:function(res){
            var statu = res.authSetting;
            if(!statu['scope.userLocation']){
              wx.showModal({
                title: '是否授权当前位置',
                content: '需要获取您的地理位置，请确认授权，否则地图功能将无法使用',
                success:function(tip){
                  if(tip.confirm){
                    wx.openSetting({
                      success:function(data){
                        if(data.authSetting["scope.userLocation"] === true){
                          
                          wx.showToast({
                            title: '授权成功',
                            icon: 'none',
                            duration: 1000
                        })
                        // that.onLocation()
                        }
                      }
                    })
                  }else{
                    wx.showToast({
                      title: '授权失败',
                      icon: 'none',
                      duration: 1000
                  })
                  }
                }
              })
            }
          },
          complete: (res) => {},
        })
      },
      complete: function () {
        // complete
      }
    })
    // 地图选择
   
  

  },
  /*
   ***input框输入
   */
  bindInputAddress(e) { //门牌号
    this.data.form.address = e.detail.value
    this.setData({
      form: this.data.form
    })
  },
  bindInputName(e) { //姓名
    this.data.form.name = e.detail.value
    this.setData({
      form: this.data.form
    })
  },
  bindInputMobile(e) { //手机号
    this.data.form.mobile = e.detail.value
    this.setData({
      form: this.data.form
    })
  },
  bindSwitchChange(e) { //收货地址默认
    if (e.detail.value) { //选中的  为true
      this.data.form.state = 1;
      this.setData({
        form: this.data.form
      })
    } else {
      this.data.form.state = 2;
      this.setData({
        form: this.data.form
      })
    }
  },
  //保存
  bindClickSumbit() {
    if (this.data.juliBool) {
      var reg = /^1\d{10}$/ //检测手机号
      if (reg.test(this.data.form.mobile)) {
        if (this.data.id == -1) { //保存
          this.data.form.userid = wx.getStorageSync('userid')
          postAddressAdd(this.data.form).then(res => {
            if (res.state == 0) {
              wx.showToast({
                title: '添加成功',
                icon: 'success',
                duration: 2000
              })
              setTimeout(function () {
                wx.navigateBack()
              }, 1000)
            }
          })
        } else {
          postAddressUpdate(this.data.form).then(res => {
            if (res.state == 0) {
              wx.showToast({
                title: '添加成功',
                icon: 'success',
                duration: 2000
              })
              setTimeout(function () {
                wx.navigateBack()
              }, 1000)
            }
          })
        }
      } else {
        wx.showToast({
          title: '请填写正确手机号',
          icon: 'none',
          duration: 2000
        })
      }

    } else {
      wx.showModal({
        title: '提示',
        content: '此距离不在配送范围内',
        showCancel: false,
        cancelColor: '#DF1A1A',
        success(res) {

        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})